<template>
  <MyModal ref="myModal" title="添加分组" :top="35">
    <template #content>
      <div style="display: grid; place-items: center">
        <a-form ref="loginForm" :model="formState" style="width: 360px">
          <a-form-item has-feedback name="groupName" label="分组名称">
            <a-input
              v-model:value="formState.groupName"
              placeholder="分组名称"
              autocomplete="off"
            ></a-input>
          </a-form-item>

          <a-form-item style="text-align: center">
            <a-button type="primary" block @click="handleSubmit" style="border-radius: 6px">
              确定
            </a-button>
          </a-form-item>
        </a-form>
      </div>
    </template>
  </MyModal>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import * as accountApi from '@/api/account'
import { message } from 'ant-design-vue'

const myModal = ref()

let onlyType = ref<string>('wx')
const showModal = (type = 'wx') => {
  myModal.value?.showModal()
  onlyType.value = type
}

const emit = defineEmits<{
  refresh: []
}>()

defineExpose({
  showModal,
})

interface formState {
  groupName: string
}
const formState = ref<formState>({
  groupName: '',
})

const handleSubmit = async () => {
  let params = {
    groupName: formState.value.groupName,
  }
  let api = null
  onlyType.value === 'wx'
    ? (api = accountApi.addWechatRobotGroup(params))
    : (api = accountApi.addFishRobotGroup(params))

  try {
    const { state, message: msg } = await api
    if (state === 200) {
      myModal.value?.cancelModal()
      emit('refresh')
    } else {
      message.error(msg)
    }
  } catch (error) {
    message.error('网络请求连接失败~')
  }
}
</script>
